//
//  BROrderStateView.m
//  Kexu
//
//  Created by IOS on 2023/7/17.
//

#import "BROrderStateView.h"
#import "BRTools.h"

@implementation BROrderStateView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

- (void)setState:(NSInteger)state {
    _state = state;
    
    // 判断是否使用新接口的 usingStatus（1-6）
    // 注意：这里需要外部传入 usingStatus 值，暂时使用 state 判断
    // 如果 state 在 1-6 范围内且不是旧接口的状态值，可能是新接口的 usingStatus
    if (state >= 1 && state <= 6 && state != 3 && state != 4 && state != 6 && state != 7) {
        // 可能是新接口的 usingStatus
        self.dotView.backgroundColor = [BRTools colorOrderUsingStatus:state];
        self.stateL.text = [BRTools strOrderUsingStatus:state];
    } else {
        // 使用旧接口的 state
        self.dotView.backgroundColor = [self getStatusInOrder:state];
        self.stateL.text = [BRTools strOrderOfType:state];
    }
}

- (void)setUsingStatus:(NSInteger)usingStatus {
    // 新增方法：直接设置 usingStatus
    _usingStatus = usingStatus;
    
    self.dotView.backgroundColor = [BRTools colorOrderUsingStatus:usingStatus];
    self.stateL.text = [BRTools strOrderUsingStatus:usingStatus];
}

- (UIColor *)getStatusInOrder:(NSInteger)status {
    
    // 如果是新接口的 usingStatus，使用新接口的颜色方法
    // 判断逻辑：status 在 1-6 范围内且不是旧接口的常见值
    if (status >= 1 && status <= 6 && status != 3 && status != 4 && status != 6 && status != 7) {
        return [BRTools colorOrderUsingStatus:status];
    }
    
    // 旧接口的 state 逻辑
    if (status == 7) {
        return UIColorMakeWithHex(@"#FF605A");
    }
    
    if (status == 2 || status == 5) {
        return UIColorMakeWithHex(@"#FFC855");
    }
    if (status == 1) {
        return UIColorMakeWithHex(@"#1295FF");
    }
    
    return UIColorMakeWithHex(@"#4CE48A");
}

#pragma mark - UI --
- (void)setUI {
    
    [self addSubview:self.stateL];
    [self.stateL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.bottom.offset(0);
        make.right.offset(0);
        make.left.offset(12);
    }];
    
    [self addSubview:self.dotView];
    [self.dotView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(6);
        make.left.offset(0);
        make.centerY.equalTo(self.stateL.mas_centerY).offset(0);
    }];
}

- (UIView *)dotView {
    
    if (_dotView == nil) {
        _dotView = [UIView new];
        _dotView.layer.cornerRadius = 3;
        _dotView.layer.masksToBounds = YES;
    }
    return _dotView;
}

- (QMUILabel *)stateL {
    
    if (_stateL == nil) {
        
        _stateL = [BRToolsView br_label:@"--" textColor:kColorTitle font:[UIFont fontB:20]];
    }
    
    return _stateL;
}

@end
